<?php


namespace App\Http\Controllers\Admin;
use App\Events\PositionChangeEvent;
use App\Exports\CompanyExport;
use App\Lib\UploadLib;
use App\Models\Company;
use App\Models\Config;
use Dingo\Api\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class CompanyController extends BaseController
{
    /**
     * 列表
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request) {
        $title = $request->input('title');
        $start_at = $request->input('start_at');
        $end_at = $request->input('end_at');
        $is_export = $request->input('is_export', false);
        if ($is_export) {
            $limit = 50000;
        } else {
            $limit = $request->get('per_page',20);
        }

        $list = Company::when($start_at, function ($q) use($start_at){
            $q->where('created_at', '>=', $start_at);
        })->when($end_at, function ($q) use($end_at){
            $end_at = date('Y-m-d', strtotime($end_at) + 24 * 3600);
            $q->where('created_at', '<=', $end_at);
        })->withCertain('type', ['value'])->withCount(['position'])->when($title, function ($q) use($title){
            $q->where('title', 'like', '%' . $title . '%');
        })->orderBy('sort', 'desc')->orderBy("updated_at", "desc")
            ->paginate($limit);

        if ($is_export) {
            $res = $list->toArray();
            return Excel::download(new CompanyExport($res['data']),  "公司列表.xlsx");
        }
        return $this->paginate($list);
    }

    /**
     * 详情
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function info(Request $request) {
        $this->validate($request, [
            'id' => 'required|exists:company'
        ]);
        $id = \request("id");
        $info = Company::withCertain('type', ['value'])->find($id);
        return $this->success($info);
    }

    /**
     * 保存|修改
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(Request $request) {
        $this->validate($request, [
            'type_id' => 'required',
//            'logo' => 'required',
            'title' => 'required',
            'desc' => 'required',
            'id' => 'filled|exists:company'
        ]);
        $input = $request->input();
        $id = \request("id");
        if ($request->hasFile('logo')) {
            $input['logo'] = UploadLib::handle($request->file('logo'), 'upload_company');
        } else {
            unset($input['logo']);
        }
        if ($id) {
            Company::where('id', $id)->update($input);
        } else {
            $info = Company::create($input);
            $id = $info->id;
        }
        return $this->success($id);
    }

    public function destroy(Request $request) {
        $this->validate($request, [
            'id' => 'required|exists:company'
        ]);
        $id = $request->input('id');
        $company = Company::find($id);
        $company->delete();
        $company->position()->delete();
        event(new PositionChangeEvent());
        return $this->success([]);
    }

    public function typeList() {
        $list = Config::where('key', Config::COMPANY_KEY_TYPE)->select("id", "value")->get();
        return $this->success($list);
    }


}

